package com.schillerchina.baselibrary.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * @author zhangzhiguo
 * @date 12/24/20 10:36 AM
 * @Description:
 */
public class ViewUtils {


    private static ViewUtils utils;

    //这是设计稿参考的宽高
    private static final float STANDARD_WIDTH = 720;
    private static final float STANDARD_HEIGHT = 1080;

    public int getmDisPlayWidth() {
        return mDisPlayWidth;
    }

    public void setmDisPlayWidth(int mDisPlayWidth) {
        this.mDisPlayWidth = mDisPlayWidth;
    }

    public int getmDisPlayHeight() {
        return mDisPlayHeight;
    }

    public void setmDisPlayHeight(int mDisPlayHeight) {
        this.mDisPlayHeight = mDisPlayHeight;
    }

    //这里是屏幕的显示宽高
    private int mDisPlayWidth;
    private int mDisPlayHeight;

    public static ViewUtils getInstance(Context context) {
        if (utils == null) {
            utils = new ViewUtils(context.getApplicationContext()); //获取application的context是防止内存泄漏
        }
        return utils;
    }

    private ViewUtils(Context context) {
        //获取屏幕的宽高
        if (mDisPlayHeight == 0 || mDisPlayWidth == 0) { //宽高还未赋值
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (manager != null) {
                DisplayMetrics displayMetrics = new DisplayMetrics();
                manager.getDefaultDisplay().getMetrics(displayMetrics); //此时displayMetrics就可以获取到屏幕宽高信息了
                if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
                    //横屏
                    mDisPlayWidth = displayMetrics.heightPixels;
                    mDisPlayHeight = displayMetrics.widthPixels - getStatusBarHeight(context);
                } else {
                    //竖屏
                    mDisPlayWidth = displayMetrics.widthPixels;
                    mDisPlayHeight = displayMetrics.heightPixels - getStatusBarHeight(context);
                }
            }
        }
    }

    //获取状态栏高度
    public int getStatusBarHeight(Context context) {
        int resID = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resID > 0) { //可以获取到状态栏高度
            return context.getResources().getDimensionPixelSize(resID);
        }
        return 0;
    }

    //获取水平方向的缩放比例
    public float getHorizontalScale() {
        return mDisPlayWidth / STANDARD_WIDTH;
    }

    //获取垂直方向的缩放比例
    public float getVerticalScale() {
        return mDisPlayHeight / STANDARD_HEIGHT;
    }

    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 4000;
    private static long lastClickTime;

    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }


}
